//
//  ZBNetWorkTool.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/11.
//  Copyright © 2016年 csdn. All rights reserved.
//

import UIKit
import AFNetworking

//使用结构体来定义接口名称
struct API {
//    线上环境
    static let onlinHost = "https://api.weibo.com/"
    
    
//    测试环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    static let homepage = "2/statuses/home_timeline.json"
    
    
}




enum ZBNetHttpMeth: String{
 
    case POST = "POST"
    case GET = "GET"
}


class ZBNetWorkTool: AFHTTPSessionManager {
    
    //声明单例对象
    static let sharedTools: ZBNetWorkTool = {
        let tools = ZBNetWorkTool(baseURL: NSURL(string: API.onlinHost))
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()

}
extension ZBNetWorkTool {
    //MARK: 加载用户token
    func loadAccessToken(code: String, finished: (result: AnyObject?, error: NSError?) -> ()) {
        
//        let url = "https://api.weibo.com/oauth2/access_token"
        
        //        设置参数
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
        
        request(.POST, urlString: API.authrizon, parameters: parameters, finished: finished)
    }
//    获取登录账号的信息
    func loadUserInfo(token: AnyObject, uid: AnyObject,finished: (result: AnyObject?, error: NSError?) -> ()){
        //        在result中获取token,uid
//        let urlString = "https://api.weibo.com/2/users/show.json"
        //        字典数组中不能添加为nil
        let parameters = ["access_token": token, "uid": uid]
        request(.GET, urlString: API.access_token, parameters: parameters, finished: finished)
   
        
    }
    
}
//MARK: 首页数据相关
extension ZBNetWorkTool {
    func loadHomePageData(token: String,max_id: Int64,since_id: Int64,finished: (result: AnyObject?, error: NSError?) -> ()) {
        var parameters = ["access_token": token]
        if max_id > 0 {
            //max_id - 1解决数据重复的问题
            parameters["max_id"] = "\(max_id)"
        }
        if since_id > 0 {
            parameters["since_id"] = "\(since_id)"
        }
        request(.GET, urlString: API.homepage, parameters: parameters, finished: finished)
    }

}


extension ZBNetWorkTool {

    //封装所有的网络请求方法 所有的网络请求都是通过这个方法和 AFN进行联系
    func request(method: ZBNetHttpMeth,urlString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?) -> ()) {
        //swift中就不能够使用利用协议 欺骗Xcode
        //为了达到封装的目的 可以将相同的闭包 抽取出来,当做参数传递给AFN框架
        let success =  { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            //执行成功的回调
            finished(result: result, error: nil)
        }
        
        //定义失败的回调
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            //执行失败的回调
            finished(result: nil, error: error)
        }
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
}



